package com.zja.dao.Impl;

import com.zja.dao.BaseDAO;
import com.zja.dao.CommentDao;
import com.zja.model.Comment;

import java.util.List;

/**
 * @author JA
 * @date: 2025/4/28 15:48
 */
public class CommentDaoImpl extends BaseDAO implements CommentDao {
    @Override
    public List<Comment> selectAll() {
        String sql = "SELECT * FROM comment";
        try {
            return executeQuery(Comment.class, sql);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Comment selectById(Long id) {
        try {
            String sql = "SELECT * FROM comment WHERE id = ?";
            return executeQueryBean(Comment.class, sql, id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public boolean insert(Comment comment) {
        try {
            String sql = "INSERT INTO comment (content, article_id, user_id, parent_comment_id, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?)";
            return executeUpdate(sql, comment.getContent(), comment.getArticleId(), comment.getUserId(), comment.getParentCommentId(), comment.getCreatedAt(), comment.getUpdatedAt());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public boolean update(Comment comment) {
        try {
            String sql = "UPDATE comment SET content = ?, article_id = ?, user_id = ?, parent_comment_id = ?, updated_at = ? WHERE id = ?";
            return executeUpdate(sql, comment.getContent(), comment.getArticleId(), comment.getUserId(), comment.getParentCommentId(), comment.getUpdatedAt(), comment.getId());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public boolean deleteById(Long id) {
        try {
            String sql = "DELETE FROM comment WHERE id = ?";
            return executeUpdate(sql, id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
